package com.lagou.context;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class BeanFactory<T> {

    private Class<T> beanName;

    public BeanFactory() {
    }

    public BeanFactory(Class<T> beanName){
        this.beanName = beanName;
    }

    public <T> Object getObject() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        Constructor<?>[] constructors = beanName.getConstructors();
        for(Constructor constructor : constructors){
            if (constructor.getParameterCount()==0){
                constructor.setAccessible(true);
                return constructor.newInstance();
            }
        }
        throw new RuntimeException(beanName+" 类 缺少无参构造");
    }
}
